/*
 *  Chrome OS Boot Time Statistics - collect and record boot time events
 *
 *  This file initially created by Google, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <metrics/bootstat.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/notifier.h>
#include <connman/service.h>
#include <connman/log.h>

static void bootstat_service_state_changed(struct connman_service *service)
{
	const char *state = connman_service_get_state(service);
	const char *type = connman_service_get_type(service);
	char event_name[BOOTSTAT_MAX_EVENT_LEN];

	(void) snprintf(event_name, sizeof (event_name),
			"network-%s-%s", type, state);
	bootstat_log(event_name);
}

static struct connman_notifier bootstat_notifier = {
	.name			= "bootstat",
	.priority		= CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
	.service_state_changed	= bootstat_service_state_changed,
};

static int bootstat_init(void)
{
	if (connman_notifier_register(&bootstat_notifier) < 0) {
		connman_error("Failed to register bootstat notifier");
		return -1;
	}
	return 0;
}

static void bootstat_finis(void)
{
	connman_notifier_unregister(&bootstat_notifier);
}

CONNMAN_PLUGIN_DEFINE(bootstat, "Chrome OS bootstat plugin", VERSION,
		CONNMAN_PLUGIN_PRIORITY_DEFAULT, bootstat_init, bootstat_finis)
